local yishi = fk.CreateSkill{
  name = "xiaobai__yishi",
}

Fk:loadTranslationTable{
  ["xiaobai__yishi"] = "益市",
  [":xiaobai__yishi"] = "出牌阶段限一次，你可以弃置手牌中一种类别的牌并展示牌堆底的等量张牌，"..
    "然后将其中其他类别的牌分配给任意名角色，因此获得牌的体力值最小的角色获得“耕积”。",
  ["#xiaobai__yishi"] = "益市：将这些牌分配给任意角色（点击取消留给自己）",
}

yishi:addEffect("active", {
  anim_type = "support",
  interaction = function (self, player)
    local basic = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeBasic end)
    local trick = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeTrick end)
    local equip = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeEquip end)
    local all_choices = {
      "basic",
      "trick",
      "equip"
    }
    local choices = {}
    if #basic > 0 then
      table.insert(choices, all_choices[1])
    end
    if #trick > 0 then
      table.insert(choices, all_choices[2])
    end
    if #equip > 0 then
      table.insert(choices, all_choices[3])
    end
    return UI.ComboBox{
      choices = choices,
      all_choices = all_choices,
    }
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  can_use = function (self, player)
    return not player:isKongcheng() and player:usedSkillTimes(yishi.name) == 0
  end,
  on_use = function (self, room, effect)
    local cards = {}
    local player = effect.from
    if self.interaction.data == "basic" then
      cards = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeBasic end)
    elseif self.interaction.data == "trick" then
      cards = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeTrick end)
    elseif self.interaction.data == "equip" then
      cards = table.filter(player:getCardIds(Player.Hand), function (cid) return Fk:getCardById(cid).type == Card.TypeEquip end)
    end
    room:throwCard(cards, yishi.name, player)
    local showCards = table.slice(room.draw_pile, #room.draw_pile + 1 - #cards, #room.draw_pile + 1)
    room:showCards(showCards, player)
    if self.interaction.data == "basic" then
      showCards = table.filter(showCards, function (cid) return Fk:getCardById(cid).type ~= Card.TypeBasic end)
    elseif self.interaction.data == "trick" then
      showCards = table.filter(showCards, function (cid) return Fk:getCardById(cid).type ~= Card.TypeTrick end)
    elseif self.interaction.data == "equip" then
      showCards = table.filter(showCards, function (cid) return Fk:getCardById(cid).type ~= Card.TypeEquip end)
    end
    -- 记录被分配到牌的角色
    local assigned_players = {}
    
    while not player.dead and #showCards > 0 do
      local tos, cards = room:askToChooseCardsAndPlayers(player, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = #showCards,
        targets = room.alive_players,
        pattern = tostring(Exppattern{ id = showCards }),
        skill_name = yishi.name,
        prompt = "#xiaobai__yishi",
        cancelable = true,
        expand_pile = showCards,
      })
      if #tos > 0 and #cards > 0 then
        for _, id in ipairs(cards) do
          table.removeOne(showCards, id)
        end
        room:moveCardTo(cards, Card.PlayerHand, tos[1], fk.ReasonGive, yishi.name, nil, false, player)
        
        -- 记录被分配到牌的角色
        if not table.contains(assigned_players, tos[1]) then
          table.insert(assigned_players, tos[1])
        end
        
        if #showCards == 0 then break end
      else
        room:moveCardTo(showCards, Card.PlayerHand, player, fk.ReasonGive, yishi.name, nil, false, player)
        
        -- 如果技能使用者获得了牌，也记录
        if not table.contains(assigned_players, player) then
          table.insert(assigned_players, player)
        end
        
        break
      end
    end
    
    -- 在被分配到牌的角色中，找到体力值最低的角色
    local min_hp_player = {}
    local min_hp = 999
    for _, p in ipairs(assigned_players) do
      if p.hp < min_hp then
        min_hp = p.hp
        min_hp_player = {p}
      elseif p.hp == min_hp then
        table.insert(min_hp_player, p)
      end
    end
    
    -- 给被分配到牌的角色中体力值最低的角色添加耕积技能
    for _, value in ipairs(min_hp_player) do
      room:handleAddLoseSkills(value, "xiaobai__gengji")
    end
  end
})

return yishi